﻿using Coldairarrow.Business.Aps;
using Coldairarrow.Entity.Aps;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.Aps
{
    /// <summary>
    /// 应用密钥
    /// </summary>
    /// <seealso cref="Coldairarrow.Api.BaseApiController" />
    [Route("/Aps/[controller]/[action]")]
    public class APS_OrganizeWorkingTimesDetailController : BaseApiController
    {
        #region DI

        public APS_OrganizeWorkingTimesDetailController(IAPS_OrganizeWorkingTimesDetailBusiness dbLinkBus, IAPS_OrganizeWorkingTimesBusiness dbLinkBusHead)
        {
            _dbLinkBus = dbLinkBus;
            _dbLinkBusHead = dbLinkBusHead;
        }

        IAPS_OrganizeWorkingTimesDetailBusiness _dbLinkBus { get; }
        IAPS_OrganizeWorkingTimesBusiness _dbLinkBusHead { get; }
        #endregion

        #region 获取

        [HttpPost]
        public async Task<AjaxResult<List<APS_OrganizeWorkingTimesDetail>>> GetDataList(PageInput<APS_OrganizeWorkingTimesDetail> input)
        {
            var list = await _dbLinkBus.GetDataListAsync(input);
            return list;

        }

        [HttpPost]
        public async Task<APS_OrganizeWorkingTimesDetail> GetTheData(IdInputDTO input)
        {
            return await _dbLinkBus.GetTheDataAsync(input.id) ?? new APS_OrganizeWorkingTimesDetail();
        }

        #endregion

        #region 提交

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="theData">保存的数据</param>
        [HttpPost]
        public async Task SaveData(APS_OrganizeWorkingTimesDetail theData)
        {
            if (theData.WorkingTimesDetailID.IsNullOrEmpty())
            {
                InitEntity(theData, "WorkingTimesDetailID");

                await _dbLinkBus.AddDataAsync(theData);
            }
            else
            {
                await _dbLinkBus.UpdateDataAsync(theData);
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="theData">保存的数据</param>
        [HttpPost]
        public async Task SaveWorkingTimesData(Models.OrganizeWorkingTimesBll theData)
        {
            if (theData.OrganizeWorkingTimes.OrganizeWorkingTimesID.IsNullOrEmpty())
            {
                InitEntity(theData.OrganizeWorkingTimes, "OrganizeWorkingTimesID");

                await _dbLinkBusHead.AddDataAsync(theData.OrganizeWorkingTimes);
            }
            else
            {
                await _dbLinkBusHead.UpdateDataAsync(theData.OrganizeWorkingTimes);
            }

            if(!string.IsNullOrEmpty(theData.OrganizeWorkingTimes.OrganizeWorkingTimesID))
            {
                if (theData.OrganizeWorkingTimesList.Count > 0)
                {
                    foreach (var item in theData.OrganizeWorkingTimesList)
                    {
                        item.OrganizeWorkingTimesID = theData.OrganizeWorkingTimes.OrganizeWorkingTimesID;
                        if (item.WorkingTimesDetailID.IsNullOrEmpty())
                        {
                            InitEntity(item, "WorkingTimesDetailID");
                            if (item.WorkingDate.HasValue)
                            {
                                item.YearMonth = item.WorkingDate.Value.ToString("yyyyMM");
                            }
                            await _dbLinkBus.AddDataAsync(item);
                        }
                        else
                        {
                            await _dbLinkBus.UpdateDataAsync(item);
                        }
                    }

                }
            }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids">id数组,JSON数组</param>
        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _dbLinkBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}